<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门" prop="deptcode">
        <el-select v-model="queryParams.deptcode" placeholder="请选择部门" clearable>
          <el-option
            v-for="dict in dict.type.dept"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="经济科目" prop="economicKm">
        <el-input
          v-model="queryParams.economicKm"
          placeholder="请输入经济科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ExpensesDemo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ExpensesDemo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:ExpensesDemo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ExpensesDemo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ExpensesDemoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="年度" align="center" prop="year" />
      <el-table-column label="月份" align="center" prop="yf" />
      <el-table-column label="日期" align="center" prop="ftdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ftdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptcode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dept" :value="scope.row.deptcode"/>
        </template>
      </el-table-column>
      <el-table-column label="经济科目" align="center" prop="economicKm" />
      <el-table-column label="包含的费用" align="center" prop="expenseType" />
      <el-table-column label="总金额" align="center" prop="zmoney" />
      <el-table-column label="经济科目金额" align="center" prop="shflag" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="flag" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ExpensesDemo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ExpensesDemo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改费用分摊模板管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年度" prop="year">
          <el-input v-model="form.year" placeholder="请输入年度" />
        </el-form-item>
        <el-form-item label="月份" prop="yf">
          <el-input v-model="form.yf" placeholder="请输入月份" />
        </el-form-item>
        <el-form-item label="日期" prop="ftdate">
          <el-date-picker clearable
            v-model="form.ftdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="部门" prop="deptcode">
          <el-select v-model="form.deptcode" placeholder="请选择部门">
            <el-option
              v-for="dict in dict.type.dept"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经济科目" prop="economicKm">
          <el-input v-model="form.economicKm" placeholder="请输入经济科目" />
        </el-form-item>
        <el-form-item label="包含的费用" prop="expenseType">
          <el-input v-model="form.expenseType" placeholder="请输入包含的费用" />
        </el-form-item>
        <el-form-item label="总金额" prop="zmoney">
          <el-input v-model="form.zmoney" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="经济科目金额" prop="shflag">
          <el-input v-model="form.shflag" placeholder="请输入经济科目金额" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入状态" />
        </el-form-item>
        <el-divider content-position="center">分摊费用子信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddJyjcSharingExpensesDataDemo">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteJyjcSharingExpensesDataDemo">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="jyjcSharingExpensesDataDemoList" :row-class-name="rowJyjcSharingExpensesDataDemoIndex" @selection-change="handleJyjcSharingExpensesDataDemoSelectionChange" ref="jyjcSharingExpensesDataDemo">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="分摊部门" prop="ftDept" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ftDept" placeholder="请输入分摊部门" />
            </template>
          </el-table-column>
          <el-table-column label="分摊金额" prop="moneys" width="130">
            <template slot-scope="scope">
              <el-input v-model="scope.row.moneys" placeholder="请输入分摊金额" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="orderNum" width="80">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNum" placeholder="请输入排序" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExpensesDemo, getExpensesDemo, delExpensesDemo, addExpensesDemo, updateExpensesDemo } from "@/api/system/ExpensesDemo"

export default {
  name: "ExpensesDemo",
  dicts: ['dept'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedJyjcSharingExpensesDataDemo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用分摊模板管理表格数据
      ExpensesDemoList: [],
      // 分摊费用子表格数据
      jyjcSharingExpensesDataDemoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptcode: null,
        economicKm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询费用分摊模板管理列表 */
    getList() {
      this.loading = true
      listExpensesDemo(this.queryParams).then(response => {
        this.ExpensesDemoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        year: null,
        yf: null,
        ftdate: null,
        deptcode: null,
        economicKm: null,
        expenseType: null,
        zmoney: null,
        shflag: null,
        orderNum: null,
        flag: null
      }
      this.jyjcSharingExpensesDataDemoList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加费用分摊模板管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExpensesDemo(id).then(response => {
        this.form = response.data
        this.jyjcSharingExpensesDataDemoList = response.data.jyjcSharingExpensesDataDemoList
        this.open = true
        this.title = "修改费用分摊模板管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.jyjcSharingExpensesDataDemoList = this.jyjcSharingExpensesDataDemoList
          if (this.form.id != null) {
            updateExpensesDemo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addExpensesDemo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除费用分摊模板管理编号为"' + ids + '"的数据项？').then(function() {
        return delExpensesDemo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 分摊费用子序号 */
    rowJyjcSharingExpensesDataDemoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 分摊费用子添加按钮操作 */
    handleAddJyjcSharingExpensesDataDemo() {
      let obj = {}
      obj.ftDept = ""
      obj.moneys = ""
      obj.orderNum = ""
      this.jyjcSharingExpensesDataDemoList.push(obj)
    },
    /** 分摊费用子删除按钮操作 */
    handleDeleteJyjcSharingExpensesDataDemo() {
      if (this.checkedJyjcSharingExpensesDataDemo.length == 0) {
        this.$modal.msgError("请先选择要删除的分摊费用子数据")
      } else {
        const jyjcSharingExpensesDataDemoList = this.jyjcSharingExpensesDataDemoList
        const checkedJyjcSharingExpensesDataDemo = this.checkedJyjcSharingExpensesDataDemo
        this.jyjcSharingExpensesDataDemoList = jyjcSharingExpensesDataDemoList.filter(function(item) {
          return checkedJyjcSharingExpensesDataDemo.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleJyjcSharingExpensesDataDemoSelectionChange(selection) {
      this.checkedJyjcSharingExpensesDataDemo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ExpensesDemo/export', {
        ...this.queryParams
      }, `ExpensesDemo_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
